<?php
namespace interface\v1;
/*
*来访登记接口类
*/

use verify;

class talentAppClass extends index
{
    private const table = 'talent_pool';
    private const title = '面试登记';
    /*选项*/
    public function search()
    {
        $this->code = 1;
        $type = $this->post('type');
        $data=  [];
        switch ($type){
            case 'nation':
                $data = explode('、', para('nationOption'));
                break;
            case 'homeTown':
                $all = find(array('table'=>'region','field'=>"distinct(province) as province"));
                foreach ($all['array'] as $array){
                    $data[] = $array['province'];
                }
                break;
            case 'education':
                $data = explode('，', para('education'));
                break;
            case 'politicsStatus':
                $data = explode('，', para('politicsStatus'));
                break;
            case 'marriage':
                $data = ['未婚', '已婚', '离异'];
                break;
            case 'exWorkPlace':
                $data = explode('，', para('exWorkPlace'));
                break;
            case 'exSalary':
                $data = explode('，', para('exSalary'));
                break;
            case 'jobId':
                $all = find(array('table'=>'job','where'=>"1=1 order by list asc"));
                foreach ($all['array'] as $array){
                    $data[] = [
                        'id'=>$array['id'],
                        'text'=>$array['name']
                    ];
                }
                break;
        }
        $this->resp['data']['option'] = $data;
    }

    /**
     * 编辑
     */
    public function commitEdit(){
        $id = $this->post('id') ?: self::getId();
        $field = [
            'name' => $this->post('name'),
            'sex' => $this->post('sex'),
            'tel' => $this->post('tel'),
            'jobId' => $this->post('jobId'),
            'exWorkPlace' => $this->post('exWorkPlace'),
            'exSalary' => $this->post('exSalary'),
            'idCard' => $this->post('idCard'),
            'birthDay' => $this->post('birthDay'),
            'nation' => $this->post('nation'),
            'homeTown' => $this->post('homeTown'),
            'height' => $this->post('height'),
            'weight' => $this->post('weight'),
            'health' => $this->post('health'),
            'education' => $this->post('education'),
            'major' => $this->post('major'),
            'college' => $this->post('college'),
            'workDate' => $this->post('workInDate'),
            'marriage' => $this->post('marriage'),
            'politicsStatus' => $this->post('politicsStatus'),
            'residenceAddress' => $this->post('residenceAddress'),
            'liveAddress' => $this->post('liveAddress'),
            'secContactName' => $this->post('secContactName'),
            'secContactTel' => $this->post('secContactTel'),
            'specialty' => $this->post('specialty'),
            'advantage' => $this->post('advantage'),
            'updateTime' => $this->time
        ];
        $annotation = [
            'name' => '姓名',
            'sex' => '性别',
            'tel' => '手机号',
            'jobId' => '应聘岗位',
        ];
        $rules = [
            'name' => 'required',
            'sex' => 'required',
            'tel' => 'required|tel',
            'jobId' => 'required',
        ];
        // 校验是否符合规则
        $vt = new verify();
        $result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
        if ($result === false) {
            $this->resp['warn'] = $vt->getErrs()[0];  // 多个规则提示最前面一条
            goto resp;
        }
        //判断
        if(!preg_match("/^([\x{4e00}-\x{9fa5}]+)$/u", $field['name']) && !preg_match("/^[a-z]+$/i", $field['name'])){
            $this->resp['warn'] = '姓名只能使用中文或英文';
        } elseif (!empty($field['idCard']) && (self::total(array('sql' => "SELECT count(*) AS total FROM " . self::table . " where idCard = '{$field['idCard']}' and id !='$id'")))) {
            $this->resp['warn'] = '已存在此身份证号的人员';
        }elseif ((self::total(array('sql' => "SELECT count(*) AS total FROM " . self::table . " where tel = '{$field['tel']}' and name = '{$field['name']}' and id !='$id'")))) {
            $this->resp['warn'] = '已存在此手机号码的同姓名人员';
        } else {
            $where = " id = '$id' ";
            $info = query(self::table, $where);
            if (!$info['id']) {
                $field = array_merge($field, [
                    'id' => $id,
                    'state' => '人才登记',
                    'workFlow' => 0,
                    'time' => $this->time
                ]);
                $json['warn'] = static::insert(["table" => self::table, "field" => $field]);
            } else {
                $json['warn'] = static::update([
                    "table" => self::table,
                    "field" => $field,
                    "where" => $where
                ]);
            }
            if (in_array($json['warn'], ["新增成功", "更新成功"])) {
                //返回
                $this->code = 1;
                $json['warn'] = '提交成功';
            }
            $this->resp['warn'] = $json['warn'];
        }
        resp:
    }
}